package ua.pp.shell.wka;

import ua.pp.shell.wka.DBAdapter.Types.Vocabulary;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class AEVocabularyActivity extends Activity{
	private  DBAdapter dbAdapter = null;
	private Vocabulary vocabulary = null;
	private Settings settings = null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        settings = new Settings(getApplicationContext());
        dbAdapter = new DBAdapter(this);    
        Intent intent = this.getIntent();
        int id = intent.getIntExtra("id", -1);
        if (id > 0) {
        	vocabulary = dbAdapter.GetVocabulary(id);
        } else {
        	vocabulary = new Vocabulary();
        }
        setContentView(R.layout.aevocabulary_activity);
        ((CheckBox)findViewById(R.id.current_vocabulary)).setChecked(settings.GetCurrentVocabulary() == vocabulary.id);
        Spinner language_from = (Spinner)findViewById(R.id.language_from);
        Translator.FillLanguageSpinner(getApplicationContext(), language_from, vocabulary.language_from);
        Spinner language_to = (Spinner)findViewById(R.id.language_to);
        Translator.FillLanguageSpinner(getApplicationContext(), language_to, vocabulary.language_to);
        ((EditText)findViewById(R.id.title)).setText(vocabulary.title);
        ((EditText)findViewById(R.id.description)).setText(vocabulary.description);
        Button ok = (Button) findViewById(R.id.ok);
        ok.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	vocabulary.title = ((EditText)findViewById(R.id.title)).getText().toString(); 
            	vocabulary.language_from = ((Spinner)findViewById(R.id.language_from)).getSelectedItem().toString();
            	vocabulary.language_to = ((Spinner)findViewById(R.id.language_to)).getSelectedItem().toString();
            	vocabulary.description = ((EditText)findViewById(R.id.description)).getText().toString();
            	if (vocabulary.title.trim().length() == 0) {
            		Toast.makeText(getApplicationContext(), R.string.title_can_not_be_empty, 2000).show();
            		return;
            	}
            	boolean add_new = false;
            	if (vocabulary.id > -1) {
            		dbAdapter.UpdateVocabulary(vocabulary);
            		add_new = false;
            	} else {
            		vocabulary.id = dbAdapter.InsertVocabulary(vocabulary);
            		add_new = true;
            	}
            	if(((CheckBox)findViewById(R.id.current_vocabulary)).isChecked()) {
            		settings.SetCurrentVocabulary(vocabulary.id);
            	} else {
            		settings.SetCurrentVocabulary(-1);
            	}
            	
            		
            	Intent intent = new Intent();
                intent.putExtra("id", vocabulary.id);
                intent.putExtra("add_new", add_new);
                setResult(RESULT_OK, intent);
                finish();  
            }
        });
        Button cancel = (Button) findViewById(R.id.cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	setResult(RESULT_CANCELED, null);
            	finish();
            }
        });
    }
}
